

import 'package:cached_network_image/cached_network_image.dart';
import 'package:dolqun_movies/Entities/Movie.dart';
import 'package:dolqun_movies/StateManagement/ModelProvider.dart';
import 'package:dolqun_movies/UI/Pages/Models/ColorThemeModel.dart';
import 'package:dolqun_movies/UI/Widgets/MyEdgeClipper.dart';
import 'package:flutter/material.dart';

class MovieView extends StatelessWidget {
  Movie movie = null;
  Function() onTab = null;
  MovieView({@required this.movie, @required this.onTab});

  @override
  Widget build(BuildContext context) {
    Widget buildingWidget = ClipRRect(
      borderRadius: BorderRadius.all(Radius.circular(8.0)),
      child: ClipPath(
        clipper: MyEdgeClipper(clipHeight: 7.0),
        child: CachedNetworkImage(imageUrl: movie.image,
          width: double.infinity,
          placeholder: (context, url) => Container(
            color: Colors.blue.withOpacity(0.04),
          ),
          fit: BoxFit.cover,
        ),
      ),
    );
    var banners = Directionality(
      textDirection: TextDirection.ltr,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          for (var value in movie.stars())
                (){
              var icon = Icons.star_border;
              if (value == StarValue.Middle) icon = Icons.star_half;
              if (value == StarValue.High) icon = Icons.star;
              return Icon(icon, color: Colors.yellow, size: 13.0,);
            }(),
//          Icon(movie.mode == 2 ? Icons.attach_money : Icons.directions_walk, size: 13.0,),
        ],
      ),
    );
    buildingWidget = Container(
      child: Stack(
        children: <Widget>[
          Positioned.fill(
            child: Column(
              children: <Widget>[
                Expanded(
                  child: buildingWidget,
                ),
                banners,
                SizedBox(
                  width: double.infinity,
                  child: Text(movie.name, maxLines: 1, style: TextStyle(fontSize: 10.0, color: Colors.blue), textAlign: TextAlign.center,),
                )
              ],
            ),
          ),
//          if (movie.mode == 2) Positioned(
//            left: 5.0,
//            top: 5.0,
//            child: Image.asset("assets/images/vip.png", width: 22.0,),
//          ),
          if (movie.mode == 2) Positioned(
            top: 5.0,
            left: 0.0,
            child: Container(
              child: Text("VIP", textAlign: TextAlign.center, style: TextStyle(color: Colors.black),),
              decoration: BoxDecoration(
                color: Colors.yellow,
                borderRadius: BorderRadius.only(
                  bottomRight: Radius.circular(10.0),
                  topRight: Radius.circular(10.0),
                )
              ),
              padding: EdgeInsets.all(3.0),
              constraints: BoxConstraints(minWidth: 40.0),
            ),
          ),
        ],
      ),
      width: 100.0,
      height: 160.0,
      margin: EdgeInsets.all(5.0),
    );
    buildingWidget = GestureDetector(
      child: buildingWidget,
      onTap: onTab,
    );
    return buildingWidget;
  }
}
enum StarValue {
  Low,
  Middle,
  High
}
extension Star on Movie {
  List<StarValue> stars (){
    var _rate = this.rate * 10;
    var stars = <StarValue>[];
    for (int index = 0; index < 5; index++) {
      var value = StarValue.Low;
      if (_rate > (index * 20) + ((20 / 3) * 1)) value = StarValue.Middle;
      if (_rate > (index * 20) + ((20 / 3) * 2)) value = StarValue.High;
      stars.add(value);
    }
    return stars;
  }
}